#!/usr/bin/python

from math import sqrt
from sys import argv

def segment(x):
    y = dy / dx * x + b
    return y

def dist(x0, y0, f, x):
    y = f(x)
    d = sqrt((x - x0) ** 2 + (y - y0) ** 2)
    return d

def hill_climb(x, delta, f):
    y0 = f(x - delta)
    y1 = f(x)
    y2 = f(x + delta)
    if y0 < y1:
        delta *= -1
        prev_y = y0
    elif y1 < y2:
        print 'min at %f, 0 iterations' % (x)
        return x
    else:
        prev_y = y2
    x += delta
    num_iters = 1
    while True:
        y = f(x + delta)
        if y > prev_y:
            print 'min at %f, %d iterations' % (x, num_iters)
            return x
        prev_y = y
        x += delta
        num_iters += 1

x0 = float(argv[1])
y0 = float(argv[2])
dy = float(argv[3])
dx = float(argv[4])
b = float(argv[5])

def f(x):
    return dist(x0, y0, segment, x)

x = hill_climb(1, .0001, f)

